import {createStore} from 'vuex'
import router from "@/router";

export default createStore({
  state: {
    hasRoutes: false, editableTabsValue: '/index', editableTabs: [{
      title: '首页', name: '/index'
    }]
  }, getters: {
    GET_TOKEN: state => {
      return sessionStorage.getItem("token")
    }, GET_MENU_LIST: state => {
      return JSON.parse(sessionStorage.getItem("menuList"))
    }, GET_PERMS: state => {
      return JSON.parse(sessionStorage.getItem("perms"))
    }, GET_USERINFO: state => {
      console.log("GET_USERINFO=" + sessionStorage.getItem("userInfo"))
      return JSON.parse(sessionStorage.getItem("userInfo"))
    }
  }, mutations: {
    SET_TOKEN: (state, token) => {
      sessionStorage.setItem("token", token)
    }, RESET_TOKEN: (state) => {
      sessionStorage.setItem("token", "")
    }, SET_MENU_LIST: (state, menuList) => {
      sessionStorage.setItem("menuList", JSON.stringify(menuList))
    }, SET_PERMS: (state, perms) => {
      sessionStorage.setItem("perms", JSON.stringify(perms))
    }, SET_USERINFO: (state, userInfo) => {
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo))
    }, ADD_TABS: (state, tab) => {
      if (state.editableTabs.findIndex(e => e.name === tab.url) === -1) {
        state.editableTabs.push({
          title: tab.name, name: tab.url
        });
      }
      state.editableTabsValue = tab.url
    }, RESET_TABS: (state) => {
      state.editableTabsValue = '/index';
      state.editableTabs = [{
        title: '首页', name: '/index'
      }]
    }, SET_ROUTES_STATE: (state, hasRoutes) => {
      state.hasRoutes = hasRoutes
    }
  }, actions: {
    // 安全退出
    logout() {
      window.sessionStorage.clear();
      this.state.hasRoutes = false
      router.replace('/login')
    }
  }, modules: {}
})
